package cn.iocoder.yudao.module.project.service.kisbom;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.module.project.controller.admin.order.vo.PreparationVO;
import cn.iocoder.yudao.module.project.dal.dataobject.order.OrderDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kisbom.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kisbom.KisBomDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kisbom.KisBomMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * bom数据 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KisBomServiceImpl implements KisBomService {

    @Resource
    private KisBomMapper kisBomMapper;

    @Override
    public String createKisBom(KisBomSaveReqVO createReqVO) {
        // 插入
        KisBomDO kisBom = BeanUtils.toBean(createReqVO, KisBomDO.class);
        kisBomMapper.insert(kisBom);
        // 返回
        return kisBom.getId();
    }

    @Override
    public void updateKisBom(KisBomSaveReqVO updateReqVO) {
        // 校验存在
        validateKisBomExists(updateReqVO.getId());
        // 更新
        KisBomDO updateObj = BeanUtils.toBean(updateReqVO, KisBomDO.class);
        kisBomMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisBom(String id) {
        // 校验存在
        validateKisBomExists(id);
        // 删除
        kisBomMapper.deleteById(id);
    }

    private void validateKisBomExists(String id) {
        if (kisBomMapper.selectById(id) == null) {
            throw exception(KIS_BOM_NOT_EXISTS);
        }
    }

    @Override
    public KisBomDO getKisBom(String id) {
        return kisBomMapper.selectById(id);
    }

    @Override
    public PageResult<KisBomDO> getKisBomPage(KisBomPageReqVO pageReqVO) {
        return kisBomMapper.selectPage(pageReqVO);
    }

    @Override
    public List<KisBomDO> selectSavedList(Map<String, Object> params) {
        List<KisBomDO> list = kisBomMapper.selectSavedList(params);
        return list;
    }

    @Override
    public void insertRecord(List<KisBomDO> insertList) {
        // 插入
        if (null != insertList && insertList.size() > 0) {
            kisBomMapper.insertList(insertList);
        }
    }

    @Override
    public void updateRecord(List<KisBomDO> updateList) {
        // 更新
        if (null != updateList && updateList.size() > 0) {
            LocalDateTime today = LocalDateTimeUtil.now();
            for (KisBomDO data : updateList) {
                data.setUpdateTime(today);
            }
            kisBomMapper.updateList(updateList);
        }
    }

    @Override
    public List<String> selectTypeList(String type) {
        List<String> result = kisBomMapper.selectTypeList(type);
        return result;
    }

    @Override
    public List<PreparationVO> selectCodeListByType(String type) {
        List<PreparationVO> result = kisBomMapper.selectCodeListByType(type);
        return result;
    }

    @Override
    public Long countBomList(Map<String, Object> params) {
        Long count = kisBomMapper.countBomList(params);
        return count;
    }

    @Override
    public List<KisBomRespVO> selectBomList(Map<String, Object> params) {
        List<KisBomRespVO> kisBomRespVOS = kisBomMapper.selectBomList(params);
        return kisBomRespVOS;
    }

    @Override
    public int deleteBomByType(String type) {
        int result = kisBomMapper.deleteBomByType(type);
        return result;
    }

}